package com.l024.framework.manager;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.provider.Settings;

import com.l024.framework.R;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.PermissionDef;
import com.yanzhenjie.permission.setting.Setting;

import java.util.List;

public class PermissionManager {

    public static void requestPermission(final Activity activity, final PermissionQuestListener listener, final @PermissionDef String... permissionGroup) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            if (AndPermission.hasPermissions(activity, permissionGroup)) {
                if (listener != null)
                    listener.onGranted();
                return;
            }
            // 没有权限，申请权限
            AndPermission.with(activity)
                    .runtime()
                    .permission(permissionGroup)
                    .onGranted(new Action<List<String>>() {
                        @Override
                        public void onAction(List<String> data) {
                            if (listener != null)
                                listener.onGranted();
                        }
                    })
                    .onDenied(new Action<List<String>>() {
                        @Override
                        public void onAction(final List<String> data) {
                            if (AndPermission.hasAlwaysDeniedPermission(activity, permissionGroup)) {
                                //替换为自己的对话框
                                final AlertDialog.Builder normalDialog =
                                        new AlertDialog.Builder(activity);
                                normalDialog.setIcon(R.drawable.icon_message);
                                normalDialog.setTitle("去申请权限");
                                normalDialog.setMessage("部分权限被你禁止了，可能误操作，可能会影响部分功能，是否去要去重新设置？");
                                normalDialog.setPositiveButton("是",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                if(dialog!=null){
                                                    dialog.dismiss();
                                                }
                                                //拒绝后不再提示
                                                startAppSettingActivity(activity);
                                            }
                                        });
                                normalDialog.setNegativeButton("否",
                                        new DialogInterface.OnClickListener() {
                                            @Override
                                            public void onClick(DialogInterface dialog, int which) {
                                                if(dialog!=null){
                                                    dialog.dismiss();
                                                }
                                                if (listener != null){
                                                    listener.onDenied(data);
                                                }
                                            }
                                        });
                                // 显示
                                normalDialog.show();
                            } else {
                                //拒绝后重复请求
                                requestPermission(activity, listener, permissionGroup);
                            }
                        }
                    })
                    .start();
        } else {
            if (listener != null)
                listener.onGranted();
        }
    }

    /**
     * 权限申请监听
     */
    public interface PermissionQuestListener{
        void onGranted(); //允许获得权限后操作
        void onDenied(List<String> data); //拒绝权限后操作
    }

    public static void startAppSettingActivity(Activity activity) {
        Intent intent = new Intent();
        intent.setAction(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
        intent.setData(Uri.parse("package:" + activity.getPackageName()));
        intent.addCategory(Intent.CATEGORY_DEFAULT);
        intent.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
        intent.addFlags(Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS);
        activity.startActivityForResult(intent, 200);
    }
}
